package net.ccbluex.liquidbounce.ui.client.hud.element.elements

import me.utils.render.RoundedUtil
import net.ccbluex.liquidbounce.LiquidBounce
import net.ccbluex.liquidbounce.features.module.modules.render.HUD
import net.ccbluex.liquidbounce.features.module.modules.render.PictureColor
import net.ccbluex.liquidbounce.features.module.modules.render.PictureColor2
import net.ccbluex.liquidbounce.font.CFontLoad
import net.ccbluex.liquidbounce.ui.client.hud.element.Border
import net.ccbluex.liquidbounce.ui.client.hud.element.Element
import net.ccbluex.liquidbounce.ui.client.hud.element.ElementInfo
import net.ccbluex.liquidbounce.ui.client.hud.element.elements.utils.QQLogoHelper2
import net.ccbluex.liquidbounce.utils.blur.BlurBuffer
import net.ccbluex.liquidbounce.utils.render.ColorUtils
import net.ccbluex.liquidbounce.utils.render.RenderUtils
import net.ccbluex.liquidbounce.utils.render.Stencil
import net.ccbluex.liquidbounce.value.FloatValue
import net.ccbluex.liquidbounce.value.IntegerValue
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.Gui
import net.minecraft.client.renderer.OpenGlHelper
import org.lwjgl.opengl.GL11
import java.awt.Color
import java.net.URL
import java.util.*
import javax.imageio.ImageIO
import kotlin.math.pow

@ElementInfo(name = "QQLogo")
class QQLogo : Element() {
    /**
     * Draw the entity.
     */
    var x1 = IntegerValue("x1", 2, 1, 50)
    var x2 = IntegerValue("x2", 2, 1, 50)
    var x3 = IntegerValue("x3", 2, 1, 150)
    var x4 = IntegerValue("x4", 2, 1, 100)
    var x5 = FloatValue("x5", 2f, 1f, 20f)
    private var got = false
    private var easingHealth = 0f
    var resourcelocation4 =
        classProvider.createResourceLocation("\u4ec0\u4e48\u73a9\u610f\u5988\u5988\u66b4\u6bd9\u662f\u4e0d\u662f\u5f53\u5e74\u6211\u628a\u4f60\u5a4a\u5b50\u5988\u7684\u81ed\u903c\u7528\u65e5\u672c\u6b66\u58eb\u5200\u5272\u4e0b\u6765\u7096\u70c2\u653e\u5230\u79bb\u5fc3\u673a\u91cc\u628a\u4f60\u5988\u7684\u81ed\u903c\u52a0\u4e0a\u5b50\u5bab\u7528\u4e00\u767e\u4e07\u8f6c\u6bcf\u79d2\u7684\u8f6c\u901f\u7ed9\u4f60\u79bb\u5fc3\u51fa\u6765\u4f60\u8fd9\u4e2a\u5e9f\u7269\u4e1c\u897f\u4e00\u6837\u6211\u770b\u4f60\u4e0d\u4e09\u4e0d\u56db\u4e0d\u75db\u4e0d\u75d2\u7684\u4e00\u76f4\u865a\u7a7a\u5bf9\u7ebf\u662f\u4e0d\u662f\u60f3\u5ff5\u725b\u97ad\u8ddf\u4f60\u5988\u7684\u81ed\u903c\u7096\u5728\u4e00\u8d77\u7684B\u5473\u9053\u4e86\u554a")
    var resourcelocation3 =
        classProvider.createResourceLocation("\u4ec0\u4e48\u73a9\u610f\u5988\u5988\u66b4\u6bd9\u662f\u4e0d\u662f\u5f53\u5e74\u6211\u628a\u4f60\u5a4a\u5b50\u5988\u7684\u81ed\u903c\u7528\u65e5\u672c\u6b66\u58eb\u5200\u5272\u4e0b\u6765\u7096\u70c2\u653e\u5230\u79bb\u5fc3\u673a\u91cc\u628a\u4f60\u5988\u7684\u81ed\u903c\u52a0\u4e0a\u5b50\u5bab\u7528\u4e00\u767e\u4e07\u8f6c\u6bcf\u79d2\u7684\u8f6c\u901f\u7ed9\u4f60\u79bb\u5fc3\u51fa\u6765\u4f60\u8fd9\u4e2a\u5e9f\u7269\u4e1c\u897f\u4e00\u6837\u6211\u770b\u4f60\u4e0d\u4e09\u4e0d\u56db\u4e0d\u75db\u4e0d\u75d2\u7684\u4e00\u76f4\u865a\u7a7a\u5bf9\u7ebf\u662f\u4e0d\u662f\u60f3\u5ff5\u725b\u97ad\u8ddf\u4f60\u5988\u7684\u81ed\u903c\u7096\u5728\u4e00\u8d77\u7684B\u5473\u9053\u4e86\u554a")
    val bufferedImage2 =
        ImageIO.read(URL("https://q.qlogo.cn/headimg_dl?dst_uin=" + QQLogoHelper2.QQNumber + "&spec=640&img_type=png"))
    val color = LiquidBounce.moduleManager.getModule(PictureColor::class.java) as PictureColor
    val color2 = LiquidBounce.moduleManager.getModule(PictureColor2::class.java) as PictureColor2
    var blurmodule = LiquidBounce.moduleManager.getModule(HUD::class.java) as HUD
    override fun drawElement(): Border? {
        updateAnim()



        var gradientColor1 = Color.WHITE
        var gradientColor2 = Color.WHITE
        var gradientColor3 = Color.WHITE
        var gradientColor4 = Color.WHITE
        gradientColor1 = ColorUtils.interpolateColorsBackAndForth(10, 20,Color(color.colorRedValue.get(), color.colorGreenValue.get(), color.colorBlueValue.get(),color.coloralpha.get()), Color(color2.colorRedValue.get(), color2.colorGreenValue.get(), color2.colorBlueValue.get(),color2.coloralpha.get()), false)
        gradientColor2 = ColorUtils.interpolateColorsBackAndForth(20, 90,Color(color2.colorRedValue.get(), color2.colorGreenValue.get(), color2.colorBlueValue.get(),color.coloralpha.get()), Color(color2.colorRedValue.get(), color2.colorGreenValue.get(), color2.colorBlueValue.get(),color2.coloralpha.get()), false)
        gradientColor3 = ColorUtils.interpolateColorsBackAndForth(20, 270, Color(color.colorRedValue.get(), color.colorGreenValue.get(), color.colorBlueValue.get(),color.coloralpha.get()),Color(color.colorRedValue.get(), color.colorGreenValue.get(), color.colorBlueValue.get(),color2.coloralpha.get()), false)
        gradientColor4 = ColorUtils.interpolateColorsBackAndForth(20, 270, Color(color2.colorRedValue.get(), color2.colorGreenValue.get(), color2.colorBlueValue.get(),color.coloralpha.get()), Color(color2.colorRedValue.get(), color2.colorGreenValue.get(), color2.colorBlueValue.get(),color2.coloralpha.get()), false)
        if (blurmodule.blurmoduleValue.get()){
            GL11.glTranslated(-renderX, -renderY, 0.0)
            BlurBuffer.blurRoundArea(renderX.toFloat()+9f, renderY.toFloat()-16f, 100f, 36f,4)

            GL11.glTranslated(renderX, renderY, 0.0)

        }

        RoundedUtil.drawGradientRound(9f,-16f,100f,36f,4f,gradientColor1,gradientColor3,gradientColor2,gradientColor1)
        CFontLoad.clientfont20.drawString(LiquidBounce.CLIENT_NAME, 30+20f, -0f, Color(255,255,255, 255).rgb)
        GL11.glDisable(2929)
        GL11.glEnable(3042)
        GL11.glDepthMask(false)
        OpenGlHelper.glBlendFunc(770, 771, 1, 0)
        GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f)
        if (!got) {
            mc.textureManager.loadTexture(
                resourcelocation4,
                classProvider.createDynamicTexture(bufferedImage2)
            )
            got = true
        }

        if (true) {
            Stencil.write(false)
            GL11.glDisable(GL11.GL_TEXTURE_2D)
            GL11.glEnable(GL11.GL_BLEND)
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA)
               //RenderUtils.fastRoundedRect(20f, -20f, 60f, 20f, 20f)
            RoundedUtil.drawGradientRound(16.toFloat(),-11f,27f,27f,4f,gradientColor1,gradientColor3,gradientColor2,gradientColor1)
            GL11.glDisable(GL11.GL_BLEND)
            GL11.glEnable(GL11.GL_TEXTURE_2D)
            Stencil.erase(true)
            mc.textureManager.bindTexture(resourcelocation3)
            GL11.glPushMatrix()
            GL11.glScaled(0.7,0.7,0.7)
            Gui.drawModalRectWithCustomSizedTexture(20, -20, 0.0f, 0.0f, 45, 60, 45f, 45f)
            GL11.glPopMatrix()

            Stencil.dispose()
        }
        GL11.glDepthMask(true)
        GL11.glDisable(3042)
        GL11.glEnable(2929)

        return Border(2.0f - 15.0f, 2.0f - 18.0f, 2.0f + 100f, 2.0f + 50.0f)
    }

    fun updateAnim() {
        easingHealth += (Objects.requireNonNull(mc.thePlayer)!!.health - easingHealth) / 2.0f.pow(10.0f - 1.5f) * RenderUtils.deltaTime
    }


}